//
//  PickNumBDListViewController.m
//  PlayBall
//
//  Created by Sun Fanglei on 13-4-15.
//
//

#import "JddPickNumBDListViewController.h"
#import "JddViewControllerManager.h"
#import "JddLotteryHallViewController.h"
@interface JddPickNumBDListViewController ()

@end

@implementation JddPickNumBDListViewController

@synthesize mutiLabel;
@synthesize moneyLabel;
@synthesize titleLabel;
@synthesize countLabel;
@synthesize bgScrollView;
@synthesize buyBtn;
@synthesize endTime;
@synthesize numberArray;
@synthesize numberListTableView;
@synthesize issue;
@synthesize passTypeLabel;
@synthesize playMethodTemp;
@synthesize mutiTextfield;

SHARED_VIEW_CONTROLLER_IMPLEMENT(JddPickNumBDListViewController);

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    NSCharacterSet *cs;
    cs = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789\n"]invertedSet];
    
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""]; //按cs分离出数组,数组按@""分离出字符串
    
    BOOL canChange = [string isEqualToString:filtered];
    
    if (!canChange) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                        message:@"倍数只能输入数字"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil,nil ];
        [alert show];
        return NO;
    }
    if ([string length] + range.location>5) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                        message:@"倍数不能大于99999"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil,nil ];
        [alert show];
        return NO;
    }
    
    if ([textField.text length]== 0) {
        if ([string isEqualToString:@"0"]) {
            return NO;
        }else {
            moneyLabel.text = [NSString stringWithFormat:@"%0.0f彩豆", count * [[textField.text stringByReplacingCharactersInRange:range withString:string] intValue] * 2];
            return YES;
        }
    }
    if (range.location>=5) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                        message:@"倍数不能大于99999"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil,nil ];
        [alert show];
        return NO;
    }else {
        moneyLabel.text = [NSString stringWithFormat:@"%0.0f彩豆", count * [[textField.text stringByReplacingCharactersInRange:range withString:string] intValue] * 2];
        return YES;
    }
}

-(BOOL)textFieldShouldClear:(UITextField *)textField{
    return  YES;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    return YES;
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapGesture:)];
    tapGestureRecognizer.delegate = self;
    [self.bgScrollView addGestureRecognizer:tapGestureRecognizer];
    
    [self.bgScrollView setContentOffset:CGPointMake(0,213) animated:YES];
    return YES;
}

- (IBAction)handleSingleTapGesture:(UITapGestureRecognizer*)sender{
    if ([mutiTextfield.text length]==0) {
        mutiTextfield.text = @"1";
    }
    [mutiTextfield resignFirstResponder];
    mutiLabel.text = [NSString stringWithFormat:@"%@倍", mutiTextfield.text];
    moneyLabel.text = [NSString stringWithFormat:@"%3.0f彩豆", count * [mutiTextfield.text floatValue] * 2];
    [self.bgScrollView removeGestureRecognizer:sender];
    [self.bgScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return DELETE;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [numberArray count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //总进球/比分过关/半全场
    if (self.playId == LOTTERY_PLAY_METHOD_BD_ZJQ || self.playId == LOTTERY_PLAY_METHOD_BD_BF || self.playId == LOTTERY_PLAY_METHOD_BD_BQC){
        JddSportListItemZJQCell *cell = [self getZJQ_BF_BQC_cellWithIndexPath:indexPath];
        return 40 + cell.numbersLbl.frame.size.height;
    }else{
        return 60.0;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //让球胜平负，胜平负
    if (self.playId == LOTTERY_PLAY_METHOD_BD) {
        JddSportListItemCell *cell = [JddSportListItemCell getInstanceView];
        JddSportNumber *ssqNumber = [numberArray objectAtIndex:indexPath.row];
        cell.HTeam.text = ssqNumber.Hteam;
        cell.VTeam.text = ssqNumber.Vteam;
        for (int b=0; b<[ssqNumber.teamArray count]; ++b) {
            NSString *sptemp = [ssqNumber.teamArray objectAtIndex:b];
            switch ([sptemp intValue]) {
                case 0:
                    cell.sp3.image = [UIImage imageNamed:@"sport_select.png"];
                    break;
                case 1:
                    cell.sp2.image = [UIImage imageNamed:@"sport_select.png"];
                    break;
                case 3:
                    cell.sp.image = [UIImage imageNamed:@"sport_select.png"];
                    break;
                default:
                    break;
            }
        }
        cell.tag = indexPath.row;
        cell.delegate = self;
        return cell;
    }
    //总进球/比分过关/半全场
    else if (self.playId == LOTTERY_PLAY_METHOD_BD_ZJQ || self.playId == LOTTERY_PLAY_METHOD_BD_BF || self.playId == LOTTERY_PLAY_METHOD_BD_BQC){
        JddSportListItemZJQCell *cell = [self getZJQ_BF_BQC_cellWithIndexPath:indexPath];
        return cell;
    }else{
        //上下单双
        JddBDSXDSListCell *cell = [JddBDSXDSListCell getInstanceView];
        JddSportNumber *ssqNumber = [numberArray objectAtIndex:indexPath.row];
        cell.HTeam.text = ssqNumber.Hteam;
        cell.VTeam.text = ssqNumber.Vteam;
        for (int b=0; b<[ssqNumber.teamArray count]; ++b) {
            NSString *sptemp = [ssqNumber.teamArray objectAtIndex:b];
            switch ([sptemp intValue]) {
                case 0:
                    cell.sp4.image = [UIImage imageNamed:@"sport_select.png"];
                    break;
                case 1:
                    cell.sp3.image = [UIImage imageNamed:@"sport_select.png"];
                    break;
                case 2:
                    cell.sp2.image = [UIImage imageNamed:@"sport_select.png"];
                    break;
                case 3:
                    cell.sp.image = [UIImage imageNamed:@"sport_select.png"];
                    break;
                default:
                    break;
            }
        }
        cell.tag = indexPath.row;
        cell.delegate = self;
        return cell;
    }
}


-(JddSportListItemZJQCell *)getZJQ_BF_BQC_cellWithIndexPath:(NSIndexPath *)indexPath{
    JddSportListItemZJQCell *cell = [JddSportListItemZJQCell getInstanceView];
    JddSportNumber *ssqNumber = [numberArray objectAtIndex:indexPath.row];
    cell.HTeam.text = ssqNumber.Hteam;
    cell.VTeam.text = ssqNumber.Vteam;
    NSMutableArray *numbersArr = [NSMutableArray arrayWithArray:[JddCommonFunction sortArray:ssqNumber.teamArray]];
    
    if ([[numbersArr lastObject] isEqualToString:@"7"]) {
        [numbersArr removeLastObject];
        [numbersArr addObject:@"7+"];
    }
    NSString *numbersStr = [numbersArr componentsJoinedByString:@","];
    if (self.playId == LOTTERY_PLAY_METHOD_BD_BQC) {
        numbersStr = [numbersArr componentsJoinedByString:@" "];
    }
    cell.numbersLbl.text = numbersStr;
    cell.tag = indexPath.row;
    cell.delegate = self;
    [self setHeightWithLable:cell.numbersLbl];
    return cell;
}

-(void)setHeightWithLable:(UILabel *) lbl
{
    lbl.numberOfLines = 0;
    CGRect frame = lbl.frame;
    UIFont *font = lbl.font;
    CGFloat width = frame.size.width;
    NSString *result = lbl.text;
    lbl.lineBreakMode = NSLineBreakByWordWrapping;
    //替换html使不影响计算高度
    result = [result stringByReplacingOccurrencesOfString:@"<font color='#1E90FF'>" withString:@""];
    result = [result stringByReplacingOccurrencesOfString:@"</font>" withString:@""];
    
    CGSize size = [result sizeWithFont:font constrainedToSize:CGSizeMake(width, MAXFLOAT)lineBreakMode:NSLineBreakByWordWrapping];
    frame.size.height = size.height;
    [lbl setFrame:frame];
}

-(void)SportListItemCellDeleteButtonClicked:(NSNumber *)rowNumber{
    for (int i=[rowNumber intValue]+1; i<[numberArray count]; ++i) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
        UITableViewCell *cell = [numberListTableView cellForRowAtIndexPath:indexPath];
        cell.tag--;
    }
    [numberArray removeObjectAtIndex:[rowNumber intValue]];
    [numberListTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:[rowNumber intValue] inSection:0]] withRowAnimation:YES];
    NSString *etStr = @"";
    if ([numberArray count]>1) {
        JddSportNumber *sportnum1 = [numberArray objectAtIndex:0];
        NSDate *date1 = [JddCommonFunction dateFromString:sportnum1.endTime];
        for (JddSportNumber *sportnum in numberArray) {
            NSDate *date2 = [JddCommonFunction dateFromString:sportnum.endTime];
            date1 =  [date2 earlierDate:date1];
        }
        endTime = date1;
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
        etStr = [dateFormatter stringFromDate:endTime];
    } else if ([numberArray count]==1){
        JddSportNumber *sportnum1 = [numberArray objectAtIndex:0];
        NSDate *date1 = [JddCommonFunction dateFromString:sportnum1.endTime];
        endTime = date1;
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
        etStr = [dateFormatter stringFromDate:endTime];
        
    }else {
        etStr = @"--------------";
    }
    self.titleLabel.text = [NSString stringWithFormat:@"共选择%d场,截止时间: %@",[numberArray count],etStr];
    playMethodTemp = [[NSMutableArray alloc]init];
    passTypeLabel.text = @"";
    [self caculateMoneyAndCount];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if ([[alertView message] isEqualToString:@"您还没有登录，请先登录"]) {
        if (buttonIndex == 0) {
            [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_LOGIN
                                                                 fromController:self
                                                                     withObject:nil
                                                                    withObject2:nil];
        }
    } else if ([[alertView message] isEqualToString:@"确认购买？"]){
        if (buttonIndex==0) {
            [self sureToBuy];
        }
    }else if ([[alertView message] isEqualToString:@"确认清空列表？"]) {
        if (buttonIndex == 0) {
            playMethodTemp = [[NSMutableArray alloc] init];
            numberArray = [[NSMutableArray alloc] init];
            mutiTextfield.text = @"1";
            passTypeLabel.text = @"";
            count = 0;
            self.titleLabel.text = @"共选择0场,截止时间: --------------";
            countLabel.text = [NSString stringWithFormat:@"%3.0f注", count];
            mutiLabel.text = [NSString stringWithFormat:@"%@倍", mutiTextfield.text];
            moneyLabel.text = [NSString stringWithFormat:@"%3.0f彩豆", count * [mutiTextfield.text floatValue] * 2];
            [numberListTableView reloadData];
        }
    }else if ([[alertView title] isEqualToString:@"退出提示"]) {
        if (buttonIndex == 0) {
            JddPickNumSportViewController *p = (JddPickNumSportViewController *)self.fromController;
            [p clearSelect:nil];
            [[JddViewControllerManager sharedInstance] popToBackViewController];
        }
    }
}

-(void) sureToBuy{
    if ([self checkValue]) {
    [JddCommonFunction setWaitingViewHidden:NO fromController:self];
    JddBuyLotteryOperation *operation2 = [[JddBuyLotteryOperation alloc] init];
    operation2.delegate = self;
    operation2.lotteryTypeId = [NSNumber numberWithInt:LOTTERY_TYPE_ID_BD];
    JddPickNumBDViewController *s = (JddPickNumBDViewController *)self.fromController;
    NSString *tste = s.issueID;
    operation2.issueId = [NSNumber numberWithInt:[tste intValue]];
    operation2.multiple = [NSNumber numberWithInt:[mutiTextfield.text intValue]];
    operation2.moneyAmount = [NSNumber numberWithFloat:count * [mutiTextfield.text floatValue] * 2];
    operation2.lotteryNumber = [self lotteryNumberList];
    [[JddGlobalParameter sharedInstance].operationQueue addOperation:operation2];
    }
}

- (NSMutableArray*)lotteryNumberList{
    NSMutableArray *paramNumberArray = [[NSMutableArray alloc] init];
	NSMutableString *lotteryNumberString = [NSMutableString string];
	NSMutableDictionary *numDic = [[NSMutableDictionary alloc] init];
	[numDic setObject:[NSNumber numberWithInt:self.playId] forKey:@"playid"];
    
	for (int i = 0; i < [numberArray count]; ++i) {
		JddSportNumber *ssqNumber = [numberArray objectAtIndex:i];
		if (i != 0) {
			[lotteryNumberString appendString:@","];
		}
		//131101001=
		NSString *code = ssqNumber.code;
        NSString *team1 = [ssqNumber.teamArray componentsJoinedByString:@"/"];
        //比分
        if (self.playId == LOTTERY_PLAY_METHOD_BD_BF) {
            team1 = [team1 stringByReplacingOccurrencesOfString :@"胜其他"withString:@"9:0"];
            team1 = [team1 stringByReplacingOccurrencesOfString :@"平其他"withString:@"9:9"];
            team1 = [team1 stringByReplacingOccurrencesOfString :@"负其他"withString:@"0:9"];
        }
        //半全场
        if (self.playId == LOTTERY_PLAY_METHOD_BD_BQC) {
            team1 = [team1 stringByReplacingOccurrencesOfString :@" "withString:@""];
            team1 = [team1 stringByReplacingOccurrencesOfString :@"胜胜"withString:@"3-3"];
            team1 = [team1 stringByReplacingOccurrencesOfString :@"胜平"withString:@"3-1"];
            team1 = [team1 stringByReplacingOccurrencesOfString :@"胜负"withString:@"3-0"];
            team1 = [team1 stringByReplacingOccurrencesOfString :@"平胜"withString:@"1-3"];
            team1 = [team1 stringByReplacingOccurrencesOfString :@"平平"withString:@"1-1"];
            team1 = [team1 stringByReplacingOccurrencesOfString :@"平负"withString:@"1-0"];
            team1 = [team1 stringByReplacingOccurrencesOfString :@"负胜"withString:@"0-3"];
            team1 = [team1 stringByReplacingOccurrencesOfString :@"负平"withString:@"0-1"];
            team1 = [team1 stringByReplacingOccurrencesOfString :@"负负"withString:@"0-0"];
        }
		[lotteryNumberString appendString:code];
		[lotteryNumberString appendString:@"="];
		//131101001=1
		//3 1 0 s p f
        [lotteryNumberString appendString:team1];
	}
	//最后增加|
	//131101001=3/1,131101002=3/1/0|
	[lotteryNumberString appendString:@"|"];
	for (int i = 0; i < [playMethodTemp count]; i++) {
		NSString *playMethod = [playMethodTemp objectAtIndex:i];
		NSString *playMethod2 = [self getPlayMethodNameWithNameStr:playMethod];
		if (i > 0) {
			[lotteryNumberString appendString:@","];
		}
		[lotteryNumberString appendString:playMethod2];
	}
	[lotteryNumberString appendString:@"|0"];
	[numDic setObject:lotteryNumberString forKey:@"number"];
	[paramNumberArray addObject:numDic];
	return paramNumberArray;
}

-(NSString *)getPlayMethodNameWithNameStr:(NSString *)name
{
    NSString *playMethod2;
    if ([name isEqualToString:@"单关"]) {
        playMethod2 = @"1*1";
    }
    else if ([name isEqualToString:@"2串1"]) {
        playMethod2 = @"2*1";
    } else if ([name isEqualToString:@"3串1"]) {
        playMethod2 = @"3*1";
    }else if ([name isEqualToString:@"4串1"]) {
        playMethod2 = @"4*1";
    }else if ([name isEqualToString:@"5串1"]) {
        playMethod2 = @"5*1";
    }else if ([name isEqualToString:@"6串1"]) {
        playMethod2 = @"6*1";
    }else if ([name isEqualToString:@"7串1"]) {
        playMethod2 = @"7*1";
    }else if ([name isEqualToString:@"8串1"]) {
        playMethod2 = @"8*1";
    }else if ([name isEqualToString:@"9串1"]) {
        playMethod2 = @"9*1";
    }else if ([name isEqualToString:@"10串1"]) {
        playMethod2 = @"10*1";
    }else if ([name isEqualToString:@"11串1"]) {
        playMethod2 = @"11*1";
    }else if ([name isEqualToString:@"12串1"]) {
        playMethod2 = @"12*1";
    }else if ([name isEqualToString:@"13串1"]) {
        playMethod2 = @"13*1";
    }else if ([name isEqualToString:@"14串1"]) {
        playMethod2 = @"14*1";
    }else if ([name isEqualToString:@"15串1"]) {
        playMethod2 = @"15*1";
    }
    return playMethod2;
}

-(void)BuyLotteryOperationResult:(NSData *)result{
    [JddCommonFunction setWaitingViewHidden:YES fromController:self];
    if (result) {
        
        NSDictionary *ReDic = [NSJSONSerialization JSONObjectWithData:result options:NSJSONReadingMutableContainers error:nil];
        if([[ReDic objectForKey:@"Code"] intValue] == 0)
        {
            
            // modify by rainy  修改投注使用接口不经过web
            NSString * fanganhao = [ReDic objectForKey:@"Data"];
            alert1 = [JddAlertView getInstanceView];
            alert1.lssueString = fanganhao;
            alert1.moneylable = moneyLabel.text;
            [alert1 alertBuyMessage:@"北京单场"];
            
//            NSString*checkUrlString = [NSString stringWithFormat:@"%@?ck=%@",BASE_SERVER_IOS_BUY_LOTTERY_URL,[ReDic objectForKey:@"Data"]];
//            NSURL *checkUrl = [NSURL URLWithString:checkUrlString];
//            [[UIApplication sharedApplication] openURL:checkUrl];
        }else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"购彩失败"                                                  message:[ReDic objectForKey:@"Data"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil,nil ];
            [alert show];
        }
//        [JddCommonFunction updateBuyLotteryListWithLotteryId:LOTTERY_TYPE_ID_BD];

    }
}

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if([touch.view isDescendantOfView:buyBtn]){
        return  NO;
    }
    return YES;
}

-(void)selectMatch:(NSMutableArray *)reslutArray{
    playMethodTemp = [NSMutableArray arrayWithArray:reslutArray];
    NSMutableString *playMethodString = [NSMutableString string];
    for (NSString *playMethod1 in playMethodTemp) {
        [playMethodString appendString:playMethod1];
        [playMethodString appendString:@","];
    }
    passTypeLabel.text = playMethodString;
    [self caculateMoneyAndCount];
}

-(void)caculateMoneyAndCount{
    count = [JddCommonFunction getCountNumberWithNumberArray:numberArray AndPlayMethodTemp:playMethodTemp];
    countLabel.text = [NSString stringWithFormat:@"%3.0f注",count];
    mutiLabel.text = [NSString stringWithFormat:@"%@倍",mutiTextfield.text];
    moneyLabel.text = [NSString stringWithFormat:@"%3.0f彩豆",count*2*[mutiTextfield.text floatValue]];
}

-(void)back
{
    if (numberArray.count >0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"退出提示"
                                                        message:@"退出后您的投注将不会保存，确认继续?"
                                                       delegate:self
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:@"取消", nil];
        [alert show];
        
    }else{
        JddPickNumSportViewController *p = (JddPickNumSportViewController *)self.fromController;
        [p clearSelect:nil];
        [[JddViewControllerManager sharedInstance] popToBackViewController];
    }
}

//-(void)setBackBtnAction
//{
//    UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
//    temporaryBarButtonItem.target = self;
//    temporaryBarButtonItem.title = @"北京单场";
//    temporaryBarButtonItem.action = @selector(back);
//    self.navigationItem.leftBarButtonItem = temporaryBarButtonItem;
//}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    [self setBackBtnAction];
    [MobClick beginLogPageView:@"BJDC-detail"];

   
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    self.titleLabel.text = [NSString stringWithFormat:@"共选择%d场,截止时间: %@",[numberArray count],[dateFormatter stringFromDate:endTime]];
    [numberListTableView reloadData];
    
    [playMethodTemp removeAllObjects];
    if (self.playId == LOTTERY_PLAY_METHOD_BD) {
        //让球胜平负
        if ([numberArray count] > 15) {
            passTypeLabel.text = [NSString stringWithFormat:@"%d串1,",15];
            [playMethodTemp addObject:[NSString stringWithFormat:@"%d串1",15]];
        }else{
            passTypeLabel.text = [NSString stringWithFormat:@"%d串1,",[numberArray count]];
            [playMethodTemp addObject:[NSString stringWithFormat:@"%d串1",[numberArray count]]];
        }
        
    }else if (self.playId == LOTTERY_PLAY_METHOD_BD_ZJQ){
        //总进球
        if ([numberArray count] > 6) {
            passTypeLabel.text = [NSString stringWithFormat:@"%d串1,",6];
            [playMethodTemp addObject:[NSString stringWithFormat:@"%d串1",6]];
        }else{
            passTypeLabel.text = [NSString stringWithFormat:@"%d串1,",[numberArray count]];
            [playMethodTemp addObject:[NSString stringWithFormat:@"%d串1",[numberArray count]]];
        }
        
    }else if (self.playId == LOTTERY_PLAY_METHOD_BD_SXDS){
        //上下单双
        if ([numberArray count] > 6) {
            passTypeLabel.text = [NSString stringWithFormat:@"%d串1,",6];
            [playMethodTemp addObject:[NSString stringWithFormat:@"%d串1",6]];
        }else{
            passTypeLabel.text = [NSString stringWithFormat:@"%d串1,",[numberArray count]];
            [playMethodTemp addObject:[NSString stringWithFormat:@"%d串1",[numberArray count]]];
        }
    }else if (self.playId == LOTTERY_PLAY_METHOD_BD_BF){
        //比分
        if ([numberArray count] > 3) {
            passTypeLabel.text = [NSString stringWithFormat:@"%d串1,",3];
            [playMethodTemp addObject:[NSString stringWithFormat:@"%d串1",3]];
        }else{
            passTypeLabel.text = [NSString stringWithFormat:@"%d串1,",[numberArray count]];
            [playMethodTemp addObject:[NSString stringWithFormat:@"%d串1",[numberArray count]]];
        }
    }else if (self.playId == LOTTERY_PLAY_METHOD_BD_BQC){
        //半全场
        if ([numberArray count] > 6) {
            passTypeLabel.text = [NSString stringWithFormat:@"%d串1,",6];
            [playMethodTemp addObject:[NSString stringWithFormat:@"%d串1",6]];
        }else{
            passTypeLabel.text = [NSString stringWithFormat:@"%d串1,",[numberArray count]];
            [playMethodTemp addObject:[NSString stringWithFormat:@"%d串1",[numberArray count]]];
        }
    }
    
    
    
    [self caculateMoneyAndCount];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [MobClick endLogPageView:@"BJDC-detail"];

    if ([mutiTextfield.text length]==0) {
        mutiTextfield.text = @"1";
    }
    [mutiTextfield resignFirstResponder];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
//    //self.navigationItem.hidesBackButton = YES;
    
    agreeLotteryForFooterView = [JddAgreeLotteryForFooterView getInstanceView];
    agreeLotteryForFooterView.fromViewController = self;
    numberListTableView.tableFooterView = agreeLotteryForFooterView;
//    [self.navigationItem setTitle:@"北京单场选队列表"];
        UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
        customLab.backgroundColor = [UIColor clearColor];
        [customLab setText:@"北京单场选队列表"];
        customLab.textAlignment = NSTextAlignmentCenter;

        [customLab setTextColor:[UIColor whiteColor]];
        customLab.font = [UIFont systemFontOfSize:19];
        self.navigationItem.titleView = customLab;
    
    playMethodTemp = [[NSMutableArray alloc] init];
    passTypeLabel.text = @"";
    
    
    
    [numberListTableView setSeparatorColor:SeparatorLineColor];
    if (IOS7) {
        [numberListTableView setSeparatorInset:UIEdgeInsetsZero];
    }
    
    
}

-(void)destoryView
{
    playMethodTemp = [[NSMutableArray alloc] init];
    numberArray = [[NSMutableArray alloc] init];
    [mutiTextfield resignFirstResponder];
    mutiTextfield.text = @"1";
    passTypeLabel.text = @"";
}



- (IBAction)coBuyBtnClick:(id)sender {
    if([self checkValue]){
    [FaQiHeMaiViewController sharedInstance].bs = [mutiTextfield.text intValue];
    [FaQiHeMaiViewController sharedInstance].lotID = LOTTERY_TYPE_ID_BD;
    JddPickNumBDViewController *ls = (JddPickNumBDViewController *)self.fromController;
    //NSString *tste = s.issueID;
    [FaQiHeMaiViewController sharedInstance].issueID = ls.issueID;
    [FaQiHeMaiViewController sharedInstance].issueName = ls.issueName;
    [FaQiHeMaiViewController sharedInstance].issueNameTitle = [NSString stringWithFormat:@"北京单场第%@期合买",ls.issueName];
    [FaQiHeMaiViewController sharedInstance].allMoney = count * [mutiTextfield.text floatValue] * 2;
    [FaQiHeMaiViewController sharedInstance].lotteryNumber = [self lotteryNumberList];
    [[JddViewControllerManager sharedInstance].navigationController pushViewController:[FaQiHeMaiViewController sharedInstance] animated:YES];
    }
}

- (void)viewDidUnload {
    [self setTitleLabel:nil];
    [self setPassTypeLabel:nil];
    [self setCountLabel:nil];
    [self setMutiLabel:nil];
    [self setMoneyLabel:nil];
    [self setNumberListTableView:nil];
    [self setBgScrollView:nil];
    [self setBuyBtn:nil];
    [self setMutiTextfield:nil];
    [super viewDidUnload];
}

- (IBAction)passTypeBtnClick:(id)sender {
    NSArray *playMethod = [[NSArray alloc] init];
    if ([numberArray count] > 0){
        playMethod = [[NSArray alloc] initWithObjects:@"单关", @"2串1",@"3串1",@"4串1",@"5串1",@"6串1",@"7串1",@"8串1", @"9串1", @"10串1", @"11串1", @"12串1", @"13串1", @"14串1", @"15串1", nil];
        int length = numberArray.count;
        //最多只有15串1
        if (length > 15) {
            length = 15;
        }
        //如果是比分，最多只有3串1
            if(self.playId == LOTTERY_PLAY_METHOD_BD_BF){
                if(length > 2){
                    length = 3;
                }
            }
            //如果是总进球 半全场 上下单双，最多只有6串1
            if(self.playId == LOTTERY_PLAY_METHOD_BD_ZJQ
               || self.playId == LOTTERY_PLAY_METHOD_BD_BQC
               || self.playId == LOTTERY_PLAY_METHOD_BD_SXDS){
                if(length>5){
                    length = 6;
                }
            }
        
        NSMutableArray *newPlayMethod = [[NSMutableArray alloc] init];
        for(int i = 0; i < length; i ++){
            [newPlayMethod addObject:[playMethod objectAtIndex:i]];
        }
        playMethod = newPlayMethod;
        CGRect rect = [[UIScreen mainScreen] bounds];
        rect.origin.x = (rect.size.width - 251) / 2.0;
        rect.origin.y = (rect.size.height - 139) / 2.0;
        rect.size.width = 251.0;
        rect.size.height = 139.0;
        comboBoxView = [JddComboBoxView getInstanceView];
        [comboBoxView setFrame:rect];
        comboBoxView.dataArray = [NSArray arrayWithArray:playMethod];
        comboBoxView.tempArray = [NSMutableArray arrayWithArray:playMethodTemp];
        comboBoxView.commitLabel.hidden = YES;
        comboBoxView.type = 0;
        comboBoxView.delegate = self;
        [comboBoxView show];
    }else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                        message:@"请至少选择一场比赛！"
                                                       delegate:self
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil ];
        [alert show];
    }
}

- (IBAction)addBtnClick:(id)sender {
    NSInteger multiple = [mutiTextfield.text intValue];
    if(multiple > 99998){
        return;
    }
    mutiTextfield.text = [NSString stringWithFormat:@"%d",++multiple];
    mutiLabel.text = [NSString stringWithFormat:@"%d倍",multiple];
    moneyLabel.text = [NSString stringWithFormat:@"%3.0f彩豆",count*multiple*2];
}

- (IBAction)reduceBtnClick:(id)sender {
    NSInteger multiple = [mutiTextfield.text intValue];
    if(multiple-- < 2){
        return;
    }
    mutiTextfield.text = [NSString stringWithFormat:@"%d",multiple];
    mutiLabel.text = [NSString stringWithFormat:@"%d倍",multiple];
    moneyLabel.text = [NSString stringWithFormat:@"%3.0f彩豆",count*multiple*2];
}

- (IBAction)clearBtnClick:(id)sender {
//    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"玩球球" message:@"确认清空列表？" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
//    [alert show];
    if (numberArray.count >0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"确认清空列表？" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
        [alert show];
    }else{
        
    }

}

- (IBAction)buyBtnClick:(id)sender {
    int multiple = [mutiTextfield.text intValue];
    if (![JddCommonFunction isLogin]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                        message:@"您还没有登录，请先登录"
                                                       delegate:self
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:@"取消",nil ];
        [alert show];
    } else {
        if (!agreeLotteryForFooterView.checkBoxBtn.selected) {
            [JddCommonFunction messageBoxWithMessage:BUY_NOT_READ];
        }
        else if ([numberArray count]==0) {
            [JddCommonFunction messageBoxWithMessage:@"购买内容为空，请先选择投注场次"];
        }
        else if (multiple == 0) {
            [JddCommonFunction messageBoxWithMessage:MULTIPLE_Tip];
        }
        else {
            if ([playMethodTemp count]==0) {
                [JddCommonFunction messageBoxWithMessage:@"请至少选择一种过关方式"];
            }else {
                [self sureToBuy];
                /*
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"玩球球"
                                                                message:@"确认购买？"
                                                               delegate:self
                                                      cancelButtonTitle:@"确定"
                                                      otherButtonTitles:@"取消",nil ];
                [alert show];*/
                
            }
        }
    }

}

- (IBAction)editDidEnd:(id)sender {
    [mutiTextfield resignFirstResponder];
}

-(BOOL)checkValue{
    if ([mutiTextfield.text intValue]>99999) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                        message:@"倍数不能大于99999"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil,nil ];
        [alert show];
        mutiTextfield.text =  @"99999";
        return NO;
    }else if ((count * [mutiTextfield.text intValue] * 2) > 2000000 ){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                        message:@"单个方案的总金额必须在2彩豆至2000000彩豆之间"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil,nil ];
        [alert show];
        return NO;
    }
    return YES;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
    return (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}

@end
